package com.yanqu.road.server.cmd.zhaoshang;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.zhaoshang.ZsProject;
import com.yanqu.road.entity.zhaoshang.ZsProjectAdditionData;
import com.yanqu.road.entity.zhaoshang.result.ZsProjectOpenResult;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.zhaoshang.UserLockerMgr;
import com.yanqu.road.server.manager.zhaoshang.ZsMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.List;


@Cmd(code = CrossProtocol.C2_ZHAO_SHANG_PROJECT_OPEN, desc = "承包项目")
public class ZsProjectOpenCmd extends CrossCommand {

    private int templateId;
    private int num;
    private UserBaseInfo userBaseInfo;
    private int titleAddition;
    private int addition;   //称号加成
    private int skillAddition;   //技能加成
    private int openNumLimit;
    private long decorationOverTime;
    private List<Integer> timeLimit;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhaoShangProto.CrossZsProjectOpenReqMsg reqMsg =  ZhaoShangProto.CrossZsProjectOpenReqMsg.parseFrom(packet.getBytes());
        templateId = reqMsg.getTemplateId();
        num = reqMsg.getNum();
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());
        titleAddition = reqMsg.getTitleAddition();
        addition = reqMsg.getAddition();
        openNumLimit = reqMsg.getOpenNumLimit();
        decorationOverTime = reqMsg.getDecorationOverTime();
        timeLimit = reqMsg.getTimeLimitList();
        skillAddition = reqMsg.getSkillAddition();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        long playerId = packet.getPlayerId();
        ZsProject myProject = new ZsProject();
        myProject.setAddition(addition);
        myProject.setTemplateId(templateId);
        myProject.setTitleAddition(titleAddition);
        myProject.setDecorationOverTime(decorationOverTime);

        ZsProjectAdditionData additionData = new ZsProjectAdditionData();
        additionData.setInsertOption();
        additionData.setCurioAddition(skillAddition);
        myProject.setAdditionData(additionData);


        ZsProjectOpenResult result = new ZsProjectOpenResult();
        result.setOpenNumLimit(openNumLimit);

        int hours = DateHelper.getHours();
        if(hours >= timeLimit.get(0) && hours < timeLimit.get(1)) {
            synchronized (UserLockerMgr.getUserLocker(playerId)) {
                ret = ZsMgr.openProject(playerId, serverId, num, userBaseInfo, templateId, myProject, result);
            }
        }else {
            ret = GameErrorCode.E_ZS_PROJECT_NO_IN_OPEN_TIME;
        }

        if (ret == 0) {
            ZsMgr.sendPlayerProjectStateInfo(serverId, playerId);
            if(result.getNotice() != null){
                ZsMgr.addProjectNotice(myProject.getProjectId(), result.getNotice());
            }
        }

        ZhaoShangProto.ZsProjectOpenRespMsgFromCross.Builder respMsg = ZhaoShangProto.ZsProjectOpenRespMsgFromCross.newBuilder();
        respMsg.setRet(ret);
        respMsg.setTemplateId(templateId);
        respMsg.setNum(num);
        respMsg.setProjectId(myProject.getProjectId());
        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_ZS_PROJECT_OPEN_FROM_CROSS, respMsg));
    }

    @Override
    public void sendData() {

    }
}
